package com.qyhl.webtv.module_news.news.complain;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * 新闻举报页面
 * @author qyhl2
 */
@Route(path = ARouterPathConstant.NEWS_COMPLAIN)
public class NewsComplainActivity extends BaseActivity {
    @BindView(R2.id.complain_title)
    TextView complainTitle;
    @BindView(R2.id.complain_toolbar)
    Toolbar complainToolbar;
    @BindView(R2.id.btn_pornographic)
    CheckBox btnPornographic;
    @BindView(R2.id.btn_advertisement)
    CheckBox btnAdvertisement;
    @BindView(R2.id.btn_title_faker)
    CheckBox btnTitleFaker;
    @BindView(R2.id.btn_old_content)
    CheckBox btnOldContent;
    @BindView(R2.id.btn_wrongly_written)
    CheckBox btnWronglyWritten;
    @BindView(R2.id.btn_type_setting)
    CheckBox btnTypeSetting;
    @BindView(R2.id.btn_other)
    CheckBox btnOther;
    @BindView(R2.id.content)
    EditText content;
    @BindView(R2.id.commit)
    TextView commit;

    private List<CheckOption> mDataList;
    private LoadingDialog.Builder builder;
    private StringBuffer option;

    private String summary;

    private String source;

    private String sourceTitle;

    private String sourceId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.news_activity_news_complain;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        inits();
    }

    @Override
    protected void setListener() {
        setListeners();
    }

    /**
     * 新闻类型:  1.普通新闻 直播 2.图片新闻 3.推荐普通新闻 4.小视频 5.电商 6.图文 7.互动 8.链接 9.内容 10.乡镇
     * 11.政务 12.专题 13.电台音频 14.电视视频 15.圈子 16.cms视频新闻 17.开票有喜 8.开票有喜(底部)
     */
    private void inits() {
        setSupportActionBar(complainToolbar);

        int type = getIntent().getIntExtra("type", 0);
        sourceTitle = getIntent().getStringExtra("title");
        sourceId = getIntent().getStringExtra("id");

        switch (type) {
            case 1:
                source = "普通新闻";
                break;
            case 2:
                source = "图片新闻";
                break;
            case 3:
                source = "REC新闻";
                break;
            case 4:
                source = "小视频";
                break;
            case 5:
                source = "电商新闻";
                break;
            case 8:
                source = "标题新闻";
                break;
            case 16:
                source = "视频新闻";
                break;
            default:
                source = "未知类型";
                break;
        }


        complainTitle.setText("我要报错");
        mDataList = new ArrayList<>();
        mDataList.add(new CheckOption(false, "低俗色情"));
        mDataList.add(new CheckOption(false, "广告"));
        mDataList.add(new CheckOption(false, "标题党"));
        mDataList.add(new CheckOption(false, "老旧重复内容"));
        mDataList.add(new CheckOption(false, "有错别字"));
        mDataList.add(new CheckOption(false, "内容排版有误"));
        mDataList.add(new CheckOption(false, "其他"));


        builder = new LoadingDialog.Builder(NewsComplainActivity.this);
        builder.setTitle("提交中...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);
    }

    /**
     * 设置监听
     */
    private void setListeners() {
        //提交
        commit.setOnClickListener(v -> {
            option = new StringBuffer();
            for (int i = 0; i < mDataList.size(); i++) {
                if (mDataList.get(i).isCheck()) {
                    option.append(mDataList.get(i).getName()).append("|");
                }
            }

            if (StringUtils.isNotEmpty(option.toString())) {
                if (StringUtils.isNotEmpty(content.getText().toString())) {
                    summary = option.append(content.getText().toString()).toString();
                } else {
                    summary = option.toString().substring(1);
                }
                doComplain();
            } else {
                Toasty.normal(NewsComplainActivity.this, "请选择举报类型！").show();
            }
        });

        //错别字
        btnWronglyWritten.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(0).setCheck(true);
            } else {
                mDataList.get(0).setCheck(false);
            }
        });

        //色情
        btnPornographic.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(0).setCheck(true);
            } else {
                mDataList.get(0).setCheck(false);
            }
        });

        //广告
        btnAdvertisement.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(1).setCheck(true);
            } else {
                mDataList.get(1).setCheck(false);
            }
        });

        //标题党
        btnTitleFaker.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(2).setCheck(true);
            } else {
                mDataList.get(2).setCheck(false);
            }
        });

        //老旧内容
        btnOldContent.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(3).setCheck(true);
            } else {
                mDataList.get(3).setCheck(false);
            }
        });

        //错别字
        btnWronglyWritten.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(4).setCheck(true);
            } else {
                mDataList.get(4).setCheck(false);
            }
        });


        //排版错误
        btnTypeSetting.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(5).setCheck(true);
            } else {
                mDataList.get(5).setCheck(false);
            }
        });


        //其他
        btnOther.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(6).setCheck(true);
            } else {
                mDataList.get(6).setCheck(false);
            }
        });

    }

    private void doComplain() {
        builder.show();
        String userPhone = CommonUtils.getInstance().getUserName();
        String nickName = CommonUtils.getInstance().getNickName();

        EasyHttp.post(NewsUrl.NEWS_COMPLAIN_URL)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("content", summary)
                .params("userPhone", userPhone)
                .params("userName", nickName)
                .params("source", source)
                .params("sourceTitle", sourceTitle)
                .params("sourceId", sourceId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        Toasty.normal(NewsComplainActivity.this, "提交失败1！").show();
                        builder.dismiss();
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult response = new Gson().fromJson(s,ApiResult.class);
                        if (response.getCode() == 200) {
                            Toasty.normal(NewsComplainActivity.this, "提交成功！").show();
                            NewsComplainActivity.this.finish();
                        } else {
                            Toasty.normal(NewsComplainActivity.this, "提交失败2！").show();
                        }
                        builder.dismiss();
                    }
                });



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (android.R.id.home == item.getItemId()) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class CheckOption implements Serializable {
        private boolean isCheck;
        private String name;

        CheckOption(boolean isCheck, String name) {
            this.isCheck = isCheck;
            this.name = name;
        }

        public boolean isCheck() {
            return isCheck;
        }

        public void setCheck(boolean check) {
            isCheck = check;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
